﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace playwrightJu3.Controls
{
    /// <summary>
    /// Tip.xaml 的交互逻辑
    /// </summary>
    public partial class Tip : Button
    {
        public Tip()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string? content = Content as string;
            var textBox = new TextBox
            {
                Text = content,
            };
            Content = textBox;
            isInputFinished = false;
            textBox.Focus();
            Keyboard.Focus(textBox);
            textBox.SelectAll();
            textBox.LostFocus += TextBox_LostFocus;
            textBox.PreviewKeyDown += TextBox_PreviewKeyDown;
        }
        private bool isInputFinished = false;
        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!isInputFinished)
            {
                if (e.Key == Key.Enter)
                {
                    FinishInput();
                }
            }

        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!isInputFinished)
            {
                FinishInput();
            }

        }

        private void FinishInput()
        {
            TextBox? textBox = Content as TextBox;
            Content = textBox?.Text;
            isInputFinished = true;
        }

    }
}
